<?php
/**
 * @name SampleModel
 * @desc sample数据获取类, 可以访问数据库，文件，其它系统等
 * @author root
 */
class TestModel extends Dao_BaseModel{
    public function __construct() {
        parent::__construct ();
    }   
    
    public function getUserList() {
//        $redis = RedisTool::getInstance();
        $user_id = 1;
        $today = date('Y-m-d 00:00:00');
        $task_id = '111111';
        
        $db_write = new PDO('mysql:host=rm-2zea58818g5sqn213.mysql.rds.aliyuncs.com;dbname=xd_app_test;port=3306;charset=utf8','songhao','Songhao123');
        
        $db_read = new PDO('mysql:host=rr-2zeue2178dhw9s547.mysql.rds.aliyuncs.com;dbname=xd_app_test;port=3306;charset=utf8','songhao','Songhao123');
        
//        $sql_del = "delete from xd_task_record where user_id={$user_id} and task_id='{$task_id}' and complete_time='{$today}'";
//        $db_write->query($sql_del);
//        $sth = $db_write->prepare($sql_del);
//        $res_del = $sth->execute();
        
//        if($res_del){
            //查询是否已经登陆
            $sql_login = "select token from xd_user_login_log where user_id={$user_id} and login_status=1 limit 1";
            $sth = $db_read->prepare($sql_login);
            $sth->execute();
            $token = $sth->fetchAll(PDO::FETCH_ASSOC);

            //查看任务完成及总数
//            $redis = RedisTool::getInstance();
//            $task_info = $redis->get('xd_task_'.$task_id);
            $task_info = [];
            if(!empty($task_info)){
                $select_task = json_decode($task_info,true);
            }else{
                $sql_select_task_main = "select task_type,finish_count,total_limit_count from xd_task where task_id='{$task_id}'";
            
        //        $select_task = $db_write->query($sql_select_task_main);
                $sth = $db_read->prepare($sql_select_task_main);
                $sth->execute();
                $select_task = $sth->fetchAll(PDO::FETCH_ASSOC);
//                $redis->set('xd_task_'.$task_id,  json_encode($select_task));
            }

            if(isset($select_task[0])){
                if($select_task[0]['total_limit_count'] > 0){
                    if($select_task[0]['finish_count'] >= $select_task[0]['total_limit_count']){
                        echo 'task count error';
                        LogTool::errorlogLog()->log('task count error');
                        exit;
                    }
                }
            }else{
                echo 'failed select task';
                LogTool::errorlogLog()->log('failed select task');
                exit;
            }

            //判断任务类型区分判断(连续签到....)

            if($token[0]['token']){  //已登陆
                //查看当日是否已完成任务
                //要加事务
                $db_write->beginTransaction();
                $sql_complete = "select complete_status,complete_time from xd_task_record where user_id={$user_id} and task_id='{$task_id}' and complete_time='{$today}' for update";
    //            $complete = $db_write->query($sql_complete);
                $sth = $db_write->prepare($sql_complete);
                $sth->execute();
                $complete = $sth->fetchAll(PDO::FETCH_ASSOC);
                $complete = [];
                if(empty($complete)){  //未完成
                    //插入任务完成记录
                    $sql_insert_task = "insert into xd_task_record (task_id,user_id,complete_status,complete_time,create_time) values ('{$task_id}',{$user_id},1,'{$today}','{$today}')";
    //                $res_task = $db_write->query($sql_insert_task);
                    $sth = $db_write->prepare($sql_insert_task);
                    $sth->execute();
                    $res_task= $db_write->lastInsertId();
                    if($res_task){
                        //修改用户金币
                        $sql_update_user = "update xd_user set total_balance=total_balance+10 where id={$user_id}";
    //                    $res_user = $db_write->query($sql_update_user);
                        $sth = $db_write->prepare($sql_update_user);
                        $res_user = $sth->execute();
                        if($res_user){

                            //修改任务完成数
                            $sql_update_task_main = "update xd_task set finish_count=finish_count+1 where task_id='{$task_id}'";
    //                        $res_task_main = $db_write->query($sql_update_task_main);
                            $sth = $db_write->prepare($sql_update_task_main);
                            $res_task_main = $sth->execute();
                            if($res_task_main){
                                echo 'success';
                                $db_write->commit();
                                //加通知

                                exit;
                            }else{
                                echo 'update task main failed';
                                LogTool::errorlogLog()->log('update task main failed');
                                //回滚
                                $db_write->rollBack();
                                exit;
                            }
                        }else{
                            echo 'update user failed';
                            LogTool::errorlogLog()->log('update user failed');
                            //回滚
                            $db_write->rollBack();
                            exit;
                        }
                    }else{
                        echo 'update task failed';
                        LogTool::errorlogLog()->log('update task failed');
                        //回滚
                        $db_write->rollBack();
                        exit;
                    }
                }else{
                    echo 'task has completed';
                    LogTool::errorlogLog()->log('task has completed');
                    exit;
                }
            }else{
                echo 'must login';
                LogTool::errorlogLog()->log('must login');
                exit;
            }
//        }
        
    }

    public function insertSample($arrInfo) {
        return true;
    }
}
